<!--
 * @Author: 李天惊 uf_lee@163.com
 * @Date: 2022-11-10 16:37:30
 * @LastEditors: 李天惊 uf_lee@163.com
 * @LastEditTime: 2025-02-28 13:00:02
 * @FilePath: \global-brain\global-brain-admin\src\views\Shop\Withdrawal\Index.vue
 * Copyright (c) 2022 by 李天惊 uf_lee@163.com, All Rights Reserved. 
-->
<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { Dialog } from '@/components/Dialog'
import { ElButton, ElMessageBox } from 'element-plus'
import { Table } from '@/components/Table'
import {
  adminCsWithdrawalListApi,
  adminCsWithdrawalDelApi,
  adminCsWithdrawalEditStatusApi
} from '@/api/cs/withdrawal'
import { adminShopShopListeApi } from '@/api/cs/shop'
import { useTable } from '@/hooks/web/useTableTest'
import See from './components/See.vue'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { useIcon } from '@/hooks/web/useIcon'
// 获取字典
import { useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
const dict: any = wsCache.get('dict')
// 字典通过value获取label
const getDictLabel = (type, value) => {
  let data = dict[type].find((item) => {
    return item.value == value
  })
  return data ? data.label : ''
}

// const iconEdit = useIcon({ icon: 'ep:edit' })
const iconSee = useIcon({ icon: 'ep:view' })
// const iconDel = useIcon({ icon: 'ep:delete' })
const { register, tableObject, methods } = useTable<
  {
    total: number
    list: any[]
  },
  any
>({
  getListApi: adminCsWithdrawalListApi,
  response: {
    data: 'data',
    count: 'count'
  }
})

const { getList, setSearchParams } = methods

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'withdrawal_id',
    label: '主键'
  },
  {
    field: 'withdrawal_sn',
    label: '提现单编号'
  },
  {
    field: 'shop_id',
    label: '所属店铺',
    search: {
      component: 'DialogRefer',
      componentProps: {
        getListApi: adminShopShopListeApi,
        response: {
          count: 'count',
          data: 'data'
        },
        title: '选择店铺（双击选择）',
        nameField: 'shop_name',
        relNameField: 'shop_name',
        valueField: 'shop_id',
        relValueField: 'shop_id',
        crudSchemas: [
          {
            field: 'shop_name',
            label: '店铺名称',
            fixed: 'left',
            search: {
              show: true
            }
          }
        ]
      },
      show: true
    },
    form: {
      component: 'DialogRefer',
      componentProps: {
        getListApi: adminShopShopListeApi,
        response: {
          count: 'count',
          data: 'data'
        },
        title: '选择店铺（双击选择）',
        nameField: 'shop_name',
        relNameField: 'shop_name',
        valueField: 'shop_id',
        relValueField: 'shop_id',
        crudSchemas: [
          {
            field: 'shop_name',
            label: '店铺名称',
            fixed: 'left',
            search: {
              show: true
            }
          }
        ]
      },
      show: true
    },
    table: {
      show: false
    },
    detail: {
      show: false
    }
  },
  {
    field: 'shop_name',
    width: 100,
    label: '店铺名称',
    form: {
      show: false
    }
  },
  {
    field: 'amount',
    label: '金额'
  },
  {
    field: 'status',
    label: '状态',
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return cellValue != '0' ? getDictLabel('shop_withdrawal_status', cellValue) : ' '
    },
    form: {
      component: 'Select',
      componentProps: {
        options: dict.shop_withdrawal_status
      }
    },
    search: {
      component: 'Select',
      componentProps: {
        options: dict.shop_withdrawal_status
      },
      show: true
    }
  },
  {
    field: 'create_user_name',
    label: '创建人'
  },
  {
    field: 'create_time',
    label: '创建时间',
    width: 180
  },
  {
    field: 'action',
    width: 300,
    label: '操作',
    form: {
      show: false
    },
    detail: {
      show: false
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const dialogVisible = ref(false)

const dialogTitle = ref('')

const actionType = ref('')

const action = async (type: string, row: any = {}) => {
  actionType.value = type
  switch (type) {
    case 'edit':
      dialogTitle.value = '编辑'
      tableObject.currentRow = row
      dialogVisible.value = true
      break
    case 'add':
      dialogTitle.value = '新增'
      tableObject.currentRow = {}
      dialogVisible.value = true
      break
    case 'see':
      dialogTitle.value = '查看'
      tableObject.currentRow = row
      dialogVisible.value = true
      break
    case 'examine':
      dialogTitle.value = '确认支付'
      tableObject.currentRow = row
      dialogVisible.value = true
      break
    case 'del':
      ElMessageBox.confirm('确定要删除这个角色吗？', '删除', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          apiDel(row.income_id)
        })
        .catch(() => {
          console.log('这里是取消confirm弹框')
        })
      break
    default:
      console.log('未知类型')
  }
}

const apiDel = async (income_id) => {
  let delRes = await adminCsWithdrawalDelApi({ income_id: income_id })
    .catch(() => {})
    .finally(() => {
      loading.value = false
    })
  if (delRes) {
    getList()
  }
}

const loading = ref(false)

const save = async (type = actionType.value) => {
  switch (type) {
    case 'examine':
      ElMessageBox.confirm('请确认这笔金额已打款?', '审核', {
        confirmButtonText: '确认打款',
        cancelButtonText: '关闭',
        type: 'warning'
      })
        .then(async () => {
          let res = await adminCsWithdrawalEditStatusApi({
            status: '2',
            withdrawal_id: tableObject.currentRow.withdrawal_id
          })
            .catch(() => {})
            .finally(() => {})
          if (res) {
            dialogVisible.value = false
            tableObject.currentPage = 1
            getList()
          }
        })
        .catch(() => {})
      break
    default:
      console.log('未知类型')
  }
}

/****************************** 初始化 start ******************************/

const init = async () => {
  // 先调订单查看详情接口
  await getList()
}
init()

/****************************** 初始化 end ******************************/
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />

    <Table
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :border="true"
      :pagination="{
        total: tableObject.count
      }"
      @register="register"
    >
      <template #action="{ row }">
        <ElButton
          v-if="row.status != 1"
          type="success"
          :icon="iconSee"
          @click="action('see', row)"
          title="查看详情"
        >
          查看详情
        </ElButton>
        <ElButton
          v-if="row.status == 1"
          type="primary"
          @click="action('examine', row)"
          title="确认支付"
        >
          确认支付
        </ElButton>
      </template>
    </Table>
  </ContentWrap>

  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <See
      v-if="actionType == 'see' || actionType == 'examine'"
      :current-row="tableObject.currentRow"
    />

    <template #footer>
      <!-- <ElButton
        v-if="actionType == 'edit' || actionType == 'add'"
        type="primary"
        :loading="loading"
        @click="save()"
      >
        保存
      </ElButton> -->
      <ElButton v-if="tableObject.currentRow.status == 1" type="primary" @click="save('examine')">
        确认支付
      </ElButton>
      <ElButton @click="dialogVisible = false">关闭</ElButton>
    </template>
  </Dialog>
</template>
